package  
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author Joseph Waters
	 */
	public class GuiMenu extends Sprite
	{
		// Embed font for use
		[Embed(source = "../lib/verdana.ttf", fontFamily = "Verdana")]
		public var font:String;
		public var format:TextFormat = new TextFormat();
		
		public var id:uint;
		
		private var color:uint;
		private var baseWidth:int;
		private var baseHeight:int;
		private var padding:int = 5;
		private var interactionType:String = "hover";
		
		private var baseItems:uint = 0;
		
		public function GuiMenu(nX:int, nY:int, nColor:uint, nSize:int, nInteraction:String) 
		{
			// Set up menu fonts
			format.font = "Verdana";
			format.color = 0x000000;
            format.size = nSize;
			
			// Initialize x, y, color vars, and interaction type
			this.x = nX;
			this.y = nY;
			this.color = nColor;
			this.interactionType = nInteraction;
		}
		
		// Function for adding items
		public function addItem(str:String, level:int, parent:int, itemName:String, setVisible:Boolean):void {			
			var textSize:Rectangle = stringWidth(str, format);
			var newText:TextField = new TextField();
			var newItem:GuiItem = new GuiItem();
			var startX:int = 0;
			var startY:int = 0;
			
			if (!setVisible) {
				newItem.visible = false;
			}
			else {
				baseItems++;
				newItem.id = baseItems;
			}
			
			newItem.graphics.beginFill(this.color);
			newItem.graphics.drawRect(startX, startY, textSize.width + padding, textSize.height + padding);
			newItem.graphics.endFill();

			newText.selectable = false;
			newText.x = startX;
			newText.y = startY;
			newText.width = textSize.width + padding;
			newText.height = textSize.height + padding;
			newText.text = str;
			newText.setTextFormat(format);
			
			newItem.addChild(newText);
			
			newItem.parentId = parent;
			newItem.level = level;
			newItem.itemName = itemName;
			
			if (level > 1) {
				for (var a:uint = 0; a < this.numChildren; a++) {
					if (GuiItem(this.getChildAt(a)).id == parent) {
						newItem.x = getChildAt(a).x + 5;
						newItem.y = getChildAt(a).y + getChildAt(a).height + Math.floor(padding / 2);
						
						GuiItem(this.getChildAt(a)).children++;
						newItem.id = GuiItem(this.getChildAt(a)).children;
						GuiItem(this.getChildAt(a)).addChild(newItem);
					}
				}
			}
			else {
				this.addChild(newItem);
			}
			
			switch(interactionType) {
				default:
					newItem.addEventListener(MouseEvent.MOUSE_OVER, showChildren, false, 0, false);
					newItem.addEventListener(MouseEvent.MOUSE_OUT, hideChildren, false, 0, false);
					break;
			}
		}
		
		// Function to get the width of strings using a textField and rect
		public function stringWidth(str:String, format:TextFormat):Rectangle {
			var textField:TextField = new TextField();
			textField.defaultTextFormat = format;
			textField.text = str;
			
			return new Rectangle(0, 0, textField.textWidth, textField.textHeight);
		}
		
		// Show items of parent on interaction
		public function showChildren(e:MouseEvent):void {
			if (e.target.parent.numChildren > 0) {
				for (var a:uint = 1; a < e.target.parent.numChildren; a++) {	
					GuiItem(e.target.parent.getChildAt(a)).visible = true;
				}
			}
		}
		
		// Hide items of parent on interaction
		public function hideChildren(e:MouseEvent):void {
			if (e.target.parent.numChildren > 0) {
				for (var b:uint = 1; b < e.target.parent.numChildren; b++) {
					GuiItem(e.target.parent.getChildAt(b)).visible = false;
				}
			}
		}
		
		// Remove items of this menu if called
		public function removeSubItems():void {
			var guiParent:GuiItem = GuiItem(this.getChildAt(0));
			if (guiParent.numChildren > 1) {
				var children:uint = guiParent.numChildren;
				for (var a:uint = 1; a <= children - 1; a++) {
					guiParent.removeChildAt(1);
				}
			}
		}
	}

}